import React from 'react';
import util from 'util';
import {Table } from 'antd';
import sys_dict from "sys_dict";

// 服务管理
class Service extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            tblSource: [],
            totalCount: 0,
            isLoading: false,
        };
        // 初始化查询参数
        this.searchParams={
            "page": 1,
            "rows": sys_dict.defaultPageSize.toString()
        };
    }
    // 渲染之后
    componentDidMount() {
        this.refresh(this.searchParams);
    }

    // 刷新123qwe
    refresh(paramdic) {
        // 打开等待框
        this.setState({isLoading: true});
        util.httpRequest({
            type: "POST", url: 'task050101.json', params: paramdic,
            //plattype: 'app/',
            success: function (result) {
                if (result.ret_code == "0000") {
                    this.setState({
                        tblSource: result.rows,
                        totalCount: result.total
                    });
                }
            }.bind(this)
            ,complete: function () {
                this.setState({
                    isLoading: false
                })
            }.bind(this)
        });
    }

    // 查询
    search(params) {
        // 分页初始化
        params.page = "1";
        params.rows = sys_dict.defaultPageSize.toString();
        params.start_date = util.date2str(params.start_date);
        params.end_date = util.date2str(params.end_date);
        this.searchParams=params;
        this.refresh(params);
    }

    // pagesize改变
    onShowSizeChange(current, pageSize) {
        console.log(current, pageSize);
        let params = this.searchParams;
        params.rows = pageSize;
        params.page = current;
        this.refresh(params);
    }
    // page改变
    onChange(page) {
        console.log(page);
        let params = this.searchParams;
        params.page = page;
        this.refresh(params);
    }

    // 初始化配置
    config() {
        const pagination = {
            total: this.state.totalCount,
            showSizeChanger: true,
            showQuickJumper: true,
            pageSizeOptions: sys_dict.pageSizeOptions,
            defaultPageSize: sys_dict.defaultPageSize,
            onShowSizeChange: this.onShowSizeChange.bind(this),
            onChange: this.onChange.bind(this)
        };
        // 返回列表配置
        return {
            tableConfig: {
                pagination: pagination,
                loading: this.state.isLoading,
                dataSource: this.state.tblSource,
                columns: [ {
                    title: '服务编号',
                    dataIndex: 'servid',
                    key: 'servid'
                }, {
                    title: 'ip地址',
                    dataIndex: 'ip',
                    key: 'ip'
                },{
                    title: '状态',
                    dataIndex: 'status',
                    key: 'status',
                    render: function (value, data, rowIndex) {
                        var str = util.convert("jobsrv_status", value);
                        return str;
                    }
                }, {
                    title: '是否Leader',
                    dataIndex: 'isleader',
                    key: 'isleader',
                    render: function (value, data, rowIndex) {
                        return value=="0"?"否":"是";
                    }
                }, {
                    title: '创建时间',
                    dataIndex: 'addtime',
                    key: 'addtime',
                    render: function (value, data, rowIndex) {
                        var str = util.dateStrFormat(value, "D")+" "+util.dateStrFormat(value, "T");
                        return str;
                    }
                }, {
                    title: '更新时间',
                    dataIndex: 'updatetime',
                    key: 'updatetime',
                    render: function (value, data, rowIndex) {
                        var str = util.dateStrFormat(value, "D")+" "+util.dateStrFormat(value, "T");
                        return str;
                    }
                }]
            }
        };
    }

    // 渲染
    render() {
        let config = this.config();
        return (
            <div>
                <Table {...config.tableConfig}></Table>
            </div>
        );
    }
}

export default Service;